﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

namespace VkSample64
{
    public class TransformSystem : ComponentSystem
    {
        private int smallType = 0;
        private int bigType = 0;
        private int pressType = 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public TransformSystem()
        {
        }

        public override void Initialization()
        {
            int m = 0;
            ContextManager.Foreach<Group>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                ref MVPMatrix mvp = ref context.GetMVPMatrix();
                if (m == 0)
                {
                    mvp.ModelMatrix = Matrix4x4.CreateRotationZ(-30 / 180.0f * MathF.PI) *
                    Matrix4x4.CreateTranslation(0, 10, 0);
                }
                else
                {
                    mvp.ModelMatrix = Matrix4x4.CreateRotationZ(-30 / 180.0f * MathF.PI) *
                    Matrix4x4.CreateTranslation(0, -5, 0);
                }
                m++;
            });
        }

        public override void Update(TimeSpan timeSpan)
        {
            bool isButtonUp = false;
            if(Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Click)
            {
                isButtonUp = true;
                pressType++;
                pressType %= 4;
                switch (pressType)
                {
                    case 0:
                        smallType = 0;
                        bigType = 1;
                        break;
                    case 1:
                        smallType = 1;
                        bigType = 0;
                        break;
                    case 2:
                        smallType = 0;
                        bigType = 0;
                        break;
                    case 3:
                        smallType = 1;
                        bigType = 1;
                        break;
                }
            }
            int index = 0;
            ContextManager.Foreach<Group>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                if (isButtonUp)
                {
                    IRenderObject render = context.GetContextRenderObject();

                    if (index == 0) context.SetCustomPropery("Index", smallType);
                    else context.SetCustomPropery("Index", bigType);
                }
                index++;
                context.PushToRenderGroup();
            });
        }

        public struct Group
        {
            public Entity Entity;
            public TransformComponent Transform;
        }
    }
}
